package com.meitianhui.convenitenbuy.module.mine.adapter;

import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.mine.model.CollectedStoresBean;
import com.meitianhui.convenitenbuy.module.mine.model.rxbusBean.CollectStoreEventBean;
import com.meitianhui.convenitenbuy.module.mine.ui.activity.CollectionActivity;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.RxBus;

import java.util.HashMap;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * Created by amy on 2017/11/20.
 */

public class CollectStoreAdapter extends BaseQuickAdapter<CollectedStoresBean.Stores, BaseViewHolder> {
    private HashMap<String, String> mDoc_url;
    private CollectionActivity context;
    private boolean isShowBtn = true;
    private boolean isShowDefaultBtn = true;

    public CollectStoreAdapter(@Nullable List<CollectedStoresBean.Stores> data, HashMap<String, String> doc_url, CollectionActivity activity) {
        super(R.layout.item_collect_store, data);
        this.context = activity;
        this.mDoc_url = doc_url;
    }

    public void setDoc_url(HashMap<String, String> doc_url) {
        this.mDoc_url = doc_url;
    }


    @Override
    protected void convert(BaseViewHolder helper, CollectedStoresBean.Stores item) {
        helper.setText(R.id.tv_collect_item_name, item.stores_name);
        helper.setText(R.id.tv_collect_item_adress, item.address);
        ImageView ivCollectItemIcon = helper.getView(R.id.iv_collect_item_icon);
        GlideUtils.setRectImage(mContext,mDoc_url.get(item.neighbor_pic_path), R.mipmap.icon_default_picture, R.mipmap.icon_default_picture, ivCollectItemIcon);

        //是否要显示右边的btn
        if (isShowBtn){
                helper.setVisible(R.id.iv_collect_item_check, false);
            //设置默认环境
            if (item.is_llm_stores.equals("Y")) {
                helper.setVisible(R.id.ll_collect_item_default, true);
            } else {
                helper.setVisible(R.id.ll_collect_item_default, false);
            }
        }else {
            if (item.ifChoose) {
                helper.setImageResource(R.id.iv_collect_item_check, R.mipmap.select_circle);
            } else {
                helper.setImageResource(R.id.iv_collect_item_check, R.mipmap.unselect_circle);
            }
            //helper.setVisible(R.id.ll_collect_item_default, false);
            if (isShowDefaultBtn){
                //设置默认环境
                if (item.is_llm_stores.equals("Y")) {
                    helper.setVisible(R.id.ll_collect_item_default, true);
                } else {
                    helper.setVisible(R.id.ll_collect_item_default, false);
                }
            }else {
                //不显示默认图标
                helper.setVisible(R.id.ll_collect_item_default, false);
            }

        }


        //判断是否点击了菜单按钮
        RxBus.get().toFlowable(CollectStoreEventBean.class).subscribe(new Consumer<CollectStoreEventBean>() {


            @Override
            public void accept(CollectStoreEventBean collectStoreEventBean) throws Exception {
                //如果是Cancle
                if (collectStoreEventBean.type.equals(Constants.COLLECT_MENU_CANCLE)) {
                    isShowBtn = false;
                    isShowDefaultBtn = false;
                    //显示设置按钮
                    helper.setVisible(R.id.ll_collect_item_default, false);
                    helper.setVisible(R.id.iv_collect_item_check, true);

                } else if (collectStoreEventBean.type.equals(Constants.COLLECT_MENU_COMFIRM)) {
                    isShowBtn = true;
                    isShowDefaultBtn  = true;
                    //显示默认门店的按钮
                    helper.setVisible(R.id.iv_collect_item_check, false);

                }else {
                    //不显示右边
                    isShowBtn = true;
                }

            }
        });


    }
}
